package com.campus.academic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.academic.job.dto.AuditNewsDTO;
import com.campus.academic.job.dto.NewsDTO;
import com.campus.academic.job.dto.NewsUpdateDTO;
import com.campus.academic.job.entity.News;
import com.campus.academic.mapper.NewsMapper;
import com.campus.academic.service.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.api.util.UserContext;
import com.campus.common.exception.ServiceException;
import com.campus.common.user.entity.pojo.User;
import com.campus.common.user.mapper.AuthMapper;
import com.campus.common.util.AliOssUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 新闻表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-16
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private AuthMapper authMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 发布新闻
     */
    @Override
    public void saveNews(NewsDTO newsDTO) {

        int userId = UserContext.getUserId();
        User user = authMapper.selectById(userId);
        if (Optional.ofNullable(user).isEmpty()) {
            throw new ServiceException("未查询到该用户信息");
        }

        if (Optional.ofNullable(newsDTO).isEmpty()) {
            throw new ServiceException("传入参数异常");
        }

        News news = new News();
        BeanUtils.copyProperties(newsDTO, news);

        if (user.getIdentity() == 2) {
            news.setPublishId(userId);
            news.setPublishDate(LocalDateTime.now());
            news.setIsDeleted(false);
            int row = newsMapper.insert(news);
            if (row < 1) {
                throw new ServiceException("发布新闻失败");
            }
        } else {
            throw new ServiceException("无权限操作");
        }


    }

    /**
     * 更新新闻信息
     */
    @Override
    public void updateNews(NewsUpdateDTO newsUpdateDTO) {
        if (Optional.ofNullable(newsUpdateDTO).isEmpty()) {
            throw new ServiceException("传入参数异常");
        }

        List<News> list = newsMapper.selectList(new LambdaQueryWrapper<News>()
                .eq(News::getId, newsUpdateDTO.getId())
                .eq(News::getIsDeleted, 0));
        if (list.isEmpty()) {
            throw new ServiceException("未查询到该新闻信息");
        }
        News news = list.get(0);
        BeanUtils.copyProperties(newsUpdateDTO, news);
        news.setChangeDate(LocalDateTime.now());
        int row = newsMapper.updateById(news);
        if (row < 1) {
            throw new ServiceException("更新新闻信息失败");
        }

    }

    /**
     * 删除新闻信息
     */
    @Override
    public void deleteNews(Integer id) {

        News news = newsMapper.selectOne(new LambdaQueryWrapper<News>()
                .eq(News::getId, id)
                .eq(News::getIsDeleted, 0));
        if (Optional.ofNullable(news).isEmpty()) {
            throw new ServiceException("未查询到该新闻信息");
        }

        news.setIsDeleted(true);
        news.setChangeDate(LocalDateTime.now());
        int row = newsMapper.updateById(news);
        if (row < 1) {
            throw new ServiceException("删除新闻信息失败");
        }
    }

    /**
     * 获取新闻信息列表
     */
    @Override
    public List<News> getNewsList() {
        //获取的是审核通过并且未删除的新闻
        return newsMapper.selectList(new LambdaQueryWrapper<News>()
                .eq(News::getIsDeleted, 0));
    }

    /**
     * 审核新闻
     */
    @Override
    public void auditNews(AuditNewsDTO auditNewsDTO) {

        int userId = UserContext.getUserId();
        User user = authMapper.selectById(userId);
        if (Optional.ofNullable(user).isEmpty()) {
            throw new ServiceException("未查询到该用户信息");
        }

        News news = newsMapper.selectOne(new LambdaQueryWrapper<News>()
                .eq(News::getId, auditNewsDTO.getNewsId())
                .eq(News::getIsDeleted, 0));
        if (Optional.ofNullable(news).isEmpty()) {
            throw new ServiceException("未查询到该新闻信息");
        }

        //管理员身份
        if (user.getIdentity() == 2) {
            //已通过
            if (auditNewsDTO.getStatus() == 1) {
                news.setStatus(auditNewsDTO.getStatus());
                news.setChangeDate(LocalDateTime.now());
                int row = newsMapper.updateById(news);
                if (row < 1) {
                    throw new ServiceException("新闻审核失败");
                }
            } else if (auditNewsDTO.getStatus() == 2) {
                //退回
                news.setStatus(auditNewsDTO.getStatus());
                news.setChangeDate(LocalDateTime.now());
                int row = newsMapper.updateById(news);
                if (row < 1) {
                    throw new ServiceException("新闻退回失败");
                }
            }
        } else {
            throw new ServiceException("无权限操作");
        }
    }

    @Override
    public String uploadImage(MultipartFile file) throws IOException {
//        int userId = UserContext.getUserId();
//        User user = authMapper.selectById(userId);
//        if (Optional.ofNullable(user).isEmpty()) {
//            throw new ServiceException("未查询到该用户信息");
//        }
        String url = aliOssUtil.upload(file);

        return url;
    }
}
